/*
 * DA4.c
 *
 * Created: 3/15/2014 11:25:42 PM
 *  Author: Vincent
 */ 


#include <avr/io.h>
#include <inttypes.h>
#define F_CPU 1000000UL		// 1 MHz
#include <util/delay.h>

int main(void)
{
	uint8_t i = 0;
	PORTB = 0x00;		// turn all LEDs off	
	DDRD |= (1<<PD5)|(1<<PD3);
	DDRB |= 1<<PB2;
						// set OC0B(PD5), 0C1B(PB2), OC2B(PD3) pin as output, required for output toggling
	TCCR0A = (1<<WGM00)|(1<<WGM01)|(1<<WGM02)|(1<<COM0B1)|(1<<COM0B0);
						// enable FAST PWM, select inverted PWM mode, top OCR0A(period).
	TCCR0B = (1<<CS01);	// prescaler = 8

	TCCR1A = (1<<WGM11)|(1<<WGM10)|(1<<WGM02)|(1<<COM1B1)|(1<<COM1B0);
						// enable FAST PWM, select inverted PWM mode, top OCR1A(period).
	TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS11);	// prescaler = 8
	
	TCCR2A = (1<<WGM22)|(1<<WGM21)|(1<<WGM20)|(1<<COM2B1)|(1<<COM2B0);
						// enable FAST PWM, select inverted PWM mode, top OCR2A(period).
	TCCR2B = (1<<CS21); // prescaler = 8

	OCR1A = OCR2A = 85+85;		//initialize pwm periods
	OCR0A = 0;
	OCR0B = OCR1B = OCR2B = 255;//initiallize pwm widths
	
	while(1)
	{
		/* dimm LED on */
		for (i=0; i<85; i++)
		{
			OCR0B = OCR1B = OCR2B++; 
			OCR0A++;
			OCR1A--;
			OCR2A++;
			_delay_ms(10); //delay 10 ms just to slow things down
		}
		for (i=0; i<85; i++)
		{
			OCR0B = OCR1B = OCR2B++; 
			OCR0A++;
			OCR1A--;
			OCR2A--;
			_delay_ms(10); //delay 10 ms just to slow things down
		}
		for (i=0; i<85; i++)
		{
			OCR0B = OCR1B = OCR2B++;  
			OCR0A++;
			OCR1A++;
			OCR2A--;
			_delay_ms(10); //delay 10 ms just to slow things down
		}
		
		/* dimm LED off */
		for (i=0; i<85; i++)
		{
			OCR0B = OCR1B = OCR2B--; 
			OCR0A--;
			OCR1A++;
			OCR2A--;
			_delay_ms(10); //delay 10 ms just to slow things down
		
		}
		for (i=0; i<85; i++)
		{
			OCR0B = OCR1B = OCR2B--; 
			OCR0A--;
			OCR1A++;
			OCR2A++;
			_delay_ms(10); //delay 10 ms just to slow things down
		}
		for (i=0; i<85; i++)
		{
			OCR0B = OCR1B = OCR2B--; 
			OCR0A--;
			OCR1A--;
			OCR2A++;
			_delay_ms(10); //delay 10 ms just to slow things down
		}

	}
	return 0;
}